<template>
  <!-- 理想的注册页面 -->
  <div class="container">
    <div class="main">
      <!-- <div class='avater_box'>
        <img src='../assets/photo.jpg'>
      </div> -->
      <div class="head">
        <div v-bind:class="flag1" @click="pclick">我要找工作</div>
        <div v-bind:class="flag2" @click="eclick">我要招聘</div>
      </div>
      <!-- status-icon -->
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="account">
          <el-input
            placeholder="请输入用户名"
            type="text"
            v-model="ruleForm.account"
            autocomplete="off"
          ></el-input>
          <!-- @input="inputLimit" -->
        </el-form-item>

        <el-form-item label="邮箱" prop="email">
          <el-input
            placeholder="请输入邮箱"
            type="text"
            v-model="ruleForm.email"
            autocomplete="off"
          ></el-input>
        </el-form-item>

        <el-form-item label="手机" prop="phone">
          <el-input
            placeholder="请输入手机号"
            v-model="ruleForm.phone"
            type="text"
            autocomplete="off"
          ></el-input>
        </el-form-item>

        <el-form-item label="密码" prop="pass">
          <el-input
            type="password"
            placeholder="请输入大于8~18位密码且至少含有2位英文字母"
            v-model="ruleForm.pass"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
          <el-input
            type="password"
            placeholder="请再次输入密码"
            v-model="ruleForm.checkPass"
            autocomplete="off"
          ></el-input>
        </el-form-item>

        <div class="email">
          <div>
            <el-form-item label="验证码" prop="checkEmail">
              <el-input
                type="text"
                v-model="ruleForm.checkEmail"
                autocomplete="off"
              ></el-input>
            </el-form-item>
          </div>
          <button class="send" v-if="showbtn" type="button" @click="sendEmail">
            发送验证码
          </button>
          <button class="send red" v-if="!showbtn" type="button">
            {{ sec }}
          </button>
        </div>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >立即注册</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
      <div v-show="tipMessage.length > 0" style="color: red">
        <p class="tip">{{ tipMessage }}</p>
      </div>
    </div>
  </div>
</template>
<script>
// import axios from "axios";
export default {
  data() {
    //判断函数
    var validateAccount = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("用户名不能为空"));
      } else {
        callback();
      }
    };
    var validateEmail = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("邮箱不能为空"));
      } else if (value !== "") {
        var re = /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/;
        if (!re.test(value)) {
          callback(new Error("请填写真实有效的邮箱!"));
        } else {
          callback();
        }
      } else {
        callback();
      }
    };
    var validatePhone = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("手机号不能为空"));
      } else if (value.length < 6) {
        callback(new Error("请输入正确的手机号"));
      } else {
        callback();
      }
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else if (value.length < 7) {
        callback(new Error("请输入密码至少8~18位且至少包含两位英文字母"));
      } else {
        callback();
      }
    };
    var validateCheckEmail = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入验证码"));
      } else {
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      // selfstyle: {
      //   width: "350px",
      // },
      flag1: "person",
      flag2: "enterprise",
      sec: "",
      //发回的验证码
      code: "",
      showbtn: true,
      tipMessage: "",
      ruleForm: {
        pass: "",
        account: "1234",
        email: "3150055730@qq.com",
        checkEmail: "",
        panduan: true,
        phone: "13395899600",
        checkPass: "",
      },
      rules: {
        pass: [{ validator: validatePass, trigger: "blur" }],
        checkPass: [{ validator: validatePass2, trigger: "blur" }],
        account: [{ validator: validateAccount, trigger: "blur" }],
        email: [{ validator: validateEmail, trigger: "blur" }],
        phone: [{ validator: validatePhone, trigger: "blur" }],
        checkEmail: [{ validator: validateCheckEmail, trigger: "blur" }],
      },
    };
  },
  methods: {
    pclick() {
      if (this.flag1 == "person") {
        this.flag2 = "enterpriseNew";
        this.flag1 = "personNew";
      } else {
        this.flag2 = "enterprise";
        this.flag1 = "person";
      }
    },
    eclick() {
      if (this.flag2 == "enterprise") {
        this.flag1 = "personNew";
        this.flag2 = "enterpriseNew";
      } else {
        this.flag2 = "enterprise";
        this.flag1 = "person";
      }
    },
    //提交设置

    async submitForm(formName) {
      //ref为标志符，以下是表单验证
      // if (this.checkemail != this.code) {
      //   this.$message({
      //     message: "验证码错误",
      //     type: "error",
      //   });
      //   // return;
      // }
      //表单验证
      if (this.flag1 == "person") {
        this.$refs[formName].validate(async (valid) => {
          if (valid) {
            try {
              // const  res = await this.$axios.post(`http://114.55.106.121:12345/auth/register`, {
              const res = await this.$axios.post(`${this.HOST}/auth/register`, {
                username: this.ruleForm.account,
                password: this.ruleForm.pass,
                isCom: false,
                email: this.ruleForm.email,
                mobile: this.ruleForm.phone,
              });
              console.log(res.data.message);
              if (res.data.code == "1") {
                console.log("注册成功");
                this.$message({
                  message: "个人账号注册成功",
                  type: "success",
                  offset: 70,
                });
                this.$router.push("/person_information");
              } else {
                this.$message({
                  message: res.data.message,
                  type: "error",
                  offset: 70,
                });
              }
            } catch (err) {
              this.$message({
                message: "发送失败",
                type: "error",
                offset: 70,
              });

              console.log(err);
            }
            // window.sessionStorage.setItem("token", res.data.token)
          } else {
            console.log("表单验证失败!!");
            return false;
          }
        });
      } else {
        console.log("enterprise");
        // this.$router.push('/enterprise_information');
        // 账号：111111
        //  密码：111111qwer
        //手机号：11111122222
        this.$refs[formName].validate(async (valid) => {
          if (valid) {
            try {
              // const  res = await this.$axios.post(`http://114.55.106.121:12345/auth/register`, {
              const res = await this.$axios.post(`${this.HOST}/auth/register`, {
                username: this.ruleForm.account,
                password: this.ruleForm.pass,
               isCom:true,
                email: this.ruleForm.email,
                mobile: this.ruleForm.phone,

              });
              console.log(res.data.message);
              if (res.data.code == "1") {
                console.log("注册成功");
                this.$message({
                  message: "企业账号注册成功",
                  type: "success",
                  offset: 70,
                });
                this.$router.push("/enterprise_information");
              } else {
                this.$message({
                  message: res.data.message,
                  type: "error",
                  offset: 70,
                });
              }
            } catch (err) {
               console.log("注册失败");
              this.$message({
                message: "发送失败",
                type: "error",
                offset: 70,
              });

              console.log(err);
            }
            // window.sessionStorage.setItem("token", res.data.token)
          } else {
            console.log("表单验证失败!!");
            return false;
          }
        });
      }
    },
    async sendEmail() {
      try {
        const res = await this.$axios.post(`http`, {
          email: this.ruleForm.email,
        });
        const info = res.data;
        console.log(info);
        //显示验证码
        if (info.num != null) {
          this.code = info.num;
          this.showbtn = false;
          //时间间隔
          this.sec = 300;
          setInterval(() => {
            this.sec = this.sec - 1;
            if (this.sec == 0) {
              this.showbtn = true;
            }
          }, 1000);
        } else {
          this.$message({
            message: "验证码发送失败",
            type: "error",
            offset: 70,
          });
        }
      } catch (err) {
        this.$message({
          message: "发送失败,请刷新页面重试！",
          type: "error",
          offset: 70,
        });
        console.log(err);
      }
    },
    //重置设置
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>
<style lang='less' scope>
.container {
  background-color: #2b4b6b;
  height: 100%;

  .main {
    width: 500px;
    height: auto;
    display: inline-block;
    margin-right: 10px;
    background-color: #fff;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    box-shadow: 0px 15px 25px rgba(0, 0, 0, 0.5);
    /*边框阴影  水平阴影0 垂直阴影15px 模糊25px 颜色黑色透明度0.5*/
    border-radius: 10px 10px 10px 10px;
    /*边框圆角，四个角均为15px*/
    .head {
      width: 500px;
      height: 100px;
      background: rgb(247, 249, 250);
      // background:#dddfe6;
      display: flex;
      text-align: center;
      font-weight: bold;
      font-size: 20px;
      border-radius: 10px 10px 0px 0px;
      // 原始状态，带有下划线
      .person {
        width: 250px;
        height: 100px;
        // background:red;
        line-height: 100px;
        padding-bottom: 8px;
        margin-left: 40px;
        color: rgb(64, 128, 255);

        font-size: 22px;
      }
      .personNew:hover {
        text-decoration: underline;
      }
      .personNew {
        width: 250px;
        height: 100px;
        // background:red;
        padding-bottom: 8px;
        margin-left: 40px;
        line-height: 100px;
        cursor: pointer;
      }
      .enterprise:hover {
        text-decoration: underline;
      }
      .enterprise {
        width: 250px;
        height: 100px;
        padding-bottom: 8px;
        margin-right: 40px;
        line-height: 100px;
        cursor: pointer;
      }
      .enterpriseNew {
        width: 250px;
        height: 100px;
        // background:red;
        line-height: 100px;
        padding-bottom: 8px;
        margin-right: 40px;
        color: rgb(64, 128, 255);
        font-size: 22px;
      }
    }
    .email {
      display: flex;
      flex-direction: row;
      justify-content: space-between;

      .send {
        height: 40px;
        width: 100px;
        margin: 5px;
        margin-right: 10px;
        border: none;
        outline: none;
        font-size: 8px;
        color: #fff;
        background: rgb(64, 158, 255);
        padding: 10px 10px;
        /* test-align: center; */
        cursor: pointer;
        /*光标呈现为指示链接的指针（一只手）*/
        border-radius: 5px;
      }
      .red {
        cursor: none;
        background-color: rgba(255, 0, 0, 0.5);
      }
    }
  }
}
</style>
